/*
 * JSFConsole - Lightweight JSF Application Monitor
 * Copyright (C) 2009  Grzegorz Bugaj
 * http://www.gregbugaj.com
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.gregbugaj.jsfconsole.agent.tracker;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.gregbugaj.jsfconsole.agent.EventType;
import com.gregbugaj.jsfconsole.agent.TraceEvent;
import com.gregbugaj.jsfconsole.agent.TraceEventListener;
import com.gregbugaj.jsfconsole.agent.TraceEventManager;
import com.gregbugaj.jsfconsole.util.Predicate;

/**
 * Track All interesting events in the lifecycle
 * @author devil
 *
 */
public class EventLogTracker implements Tracker {

	private Predicate<? super TraceEvent>  predicate;

	private TraceEventListener listener;
	public  Map<String, RequestInfo> requestMap;

	public EventLogTracker(){
		requestMap=Collections.<String, RequestInfo>emptyMap();

	}
	/* (non-Javadoc)
	 * @see com.gregbugaj.jsfdump.agent.tracker.Tracker#startMonitoring()
	 */
	public void startMonitoring(){
		requestMap=new HashMap<String, RequestInfo>();
		listener=new TraceEventListener(){
			public void trace(TraceEvent traceEvent) {							
				//Apply predicate if we have one
				if(predicate!=null && predicate.evaluate(traceEvent)==false){
					return;
				}
				//Arguments passed from our ClassTransformer
				Integer facesId=traceEvent.getSourceId();
				String fkey=facesId.toString();
				
				//Data is aggregated per each Request 
				RequestInfo requestInfo=null;
				if(requestMap.get(fkey)==null){
					requestInfo=new RequestInfo(traceEvent.getRequestPath());
					requestMap.put(fkey, requestInfo);
				}
				
				requestInfo=requestMap.get(fkey);
				
				EventInfo info=new EventInfo();
				info.setStartTime(traceEvent.getTime());
				info.setEndTime(traceEvent.getTime());
				info.setFacesId(facesId);
				info.setInfo(traceEvent.getEventType().name());
		
				requestInfo.addEventInfo(info);
			}

			public String getListenerId() {				
				return EventLogTracker.class.getName()+".TraceEventListener";
			}


		};
		
		TraceEventManager.registerListener(listener, EventType.ANY);
	}

	/* (non-Javadoc)
	 * @see com.gregbugaj.jsfdump.agent.tracker.Tracker#stopMonitoring()
	 */
	public void stopMonitoring(){
		TraceEventManager.removeListener(listener, EventType.ANY);
	}

	/* (non-Javadoc)
	 * @see com.gregbugaj.jsfdump.agent.tracker.Tracker#getEvents()
	 */
	public List<RequestInfo> getEvents(int maxCount){
		List<RequestInfo> items = Collections.<RequestInfo>emptyList();  
		Collection<RequestInfo> ri=requestMap.values();
		if(ri!=null){
			items=new ArrayList<RequestInfo>();
			items.addAll(ri);
			Collections.sort(items, RequestInfo.getComparator());
		}
		return items;
	}


	public List<RequestInfo> getEvents(int maxCount, String requestPath) {
		return getEvents(maxCount);
	}


	public void setFilter(Predicate<? super TraceEvent>  predicate) {
		this.predicate=predicate;	
	}
}
